package member;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import book.BookBean;
import book.RentalBean;
import book.ReserveBean;

import common.TableManager;

public class MailEditServlet extends HttpServlet
{
	String address=null;
	String sql=null;
	String param=null;
	String type=null;
	String msg=null;

	String[] params={"notReturn","reserve","rental","return"};

	String id=null;
	String userId=null;
	String memberId=null;
	String userName=null;
	String bookTitle=null;
	String bookId=null;
	String reserveId=null;

	RentalBean rentals=null;
	MemberBean members=null;
	BookBean books=null;
	ReserveBean reserves=null;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		msg=null;

		param=null;
		id=null;

		id=req.getParameter("id");

		try
		{
			for(int i=0;i<4;i++)
			{
				System.out.println(req.getParameter(params[i]));
				param=(req.getParameter(params[i])!=null)?(req.getParameter(params[i])):param;

				System.out.println(param);

				if(param!=null)
				{
					type=params[i];
					break;
				}
			}

			String strId=(String)req.getParameter("id");

			System.out.println(strId);

			if(params[0].equals(type)&&strId!=null)
			{
				System.out.println("params[0] : " + params[0]);
				System.out.println("type : " + type);
				System.out.println("strId : " + strId);

				int id=Integer.parseInt(strId);

				address="/member/userMemberNotReturnMail.jsp";

				rentals=(RentalBean)TableManager.getRecord(TableManager.RENTALS_TABLE, id).getBean();
				members=rentals.getMemberBean();
				books=rentals.getBookBean();
				reserves=rentals.getReserveBean();

				System.out.println("notReturn:"+reserves);

				userId=members.getUserId();
				memberId=String.valueOf(members.getId());
				userName=members.getName();
				bookTitle=books.getTitle();

				sql=null;
			}
			else if(params[1].equals(type)&&strId!=null)
			{
				int id=Integer.parseInt(strId);

				address="/member/userMemberReserveMail.jsp";

				reserves = (ReserveBean)TableManager.getRecord(TableManager.RESERVES_TABLE, id).getBean();
				members=reserves.getMemberBean();

				books=reserves.getBookBean();

				System.out.println("reserve");

				userId=members.getUserId();
				memberId=String.valueOf(members.getId());
				userName=members.getName();
				bookTitle=books.getTitle();

				sql="SELECT * FROM rentals WHERE RESERVE_ID ="+reserves.getId();
			}
			else if(params[2].equals(type)&&strId!=null)
			{
				int id=Integer.parseInt(strId);

				address="/book/RentalEntryServlet";

				reserves = (ReserveBean)TableManager.getRecord(TableManager.RESERVES_TABLE, id).getBean();
				members=reserves.getMemberBean();
				books=reserves.getBookBean();

				System.out.println("rental");

				memberId=String.valueOf(members.getId());
				bookId=String.valueOf(books.getId());
				reserveId=String.valueOf(reserves.getId());

				sql="SELECT * FROM rentals WHERE RESERVE_ID ="+reserveId;
			}
			else if(params[3].equals(type)&&strId!=null)
			{
				int id=Integer.parseInt(strId);

				rentals=(RentalBean)TableManager.getRecord(TableManager.RENTALS_TABLE, id).getBean();

				address="/book/ReturnEntryServlet?id=" + rentals.getMemberId();
			}
			else
			{
				if(params[0].equals(type)||params[3].equals(type))
				{
					address="/member/RentalListServlet";
				}
				else
				{
					address="/member/ReserveListServlet";
				}
				msg="Please choice anything...";
			}

			if(sql!=null)
			{
				System.out.println(sql);
				rentals=(RentalBean)TableManager.getRecord(TableManager.RENTALS_TABLE, sql).getBean();
			}

			Calendar reserveDate=Calendar.getInstance();
			Calendar rentalPlanDate=Calendar.getInstance();
			Calendar returnDate=Calendar.getInstance();


			if(reserves!=null)
			{
				if(reserves.getReserveDate()!=null)
				{
					reserveDate.setTime(reserves.getReserveDate());
					reserveDate.add(Calendar.MONTH, 1);
				}
			}

			if(rentals.getReturnPlanDate()!=null)
			{
				rentalPlanDate.setTime(rentals.getReturnPlanDate());
			}

			if(rentals.getReturnDate()!=null)
			{
				returnDate.setTime(rentals.getReturnDate());
			}
			//returnDate.add(Calendar.MONTH, 1);

			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");

			String reserveDateText=sdf.format(reserveDate.getTime());
			String rentalPlanDateText=sdf.format(rentalPlanDate.getTime());
			String returnDateText=sdf.format(returnDate.getTime());

			System.out.println(userId);

			req.setAttribute("id",id);
			req.setAttribute("userId", userId);
			req.setAttribute("memberId", memberId);
			req.setAttribute("userName", userName);
			req.setAttribute("bookId", bookId);
			req.setAttribute("bookTitle",bookTitle);
			req.setAttribute("reserveId",reserveId);
			req.setAttribute("reserveDate", reserveDateText);
			req.setAttribute("rentalPlanDate", rentalPlanDateText);
			req.setAttribute("returnDate", returnDateText);
		}
		catch(NumberFormatException e)
		{
			e.printStackTrace();
		}
		catch(NullPointerException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(msg!=null)
			{
				req.setAttribute("msg", msg);
			}
			req.getRequestDispatcher(address).forward(req,resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		doGet(req, resp);
	}

}
